#!/usr/bin/env python3
# PYTHON_ARGCOMPLETE_OK

import os
import argparse
import argcomplete
import actions

parser = argparse.ArgumentParser()
subparsers = parser.add_subparsers()

parser_init = subparsers.add_parser('init', help='initialize project')
parser_init.set_defaults(init=True)

parser_configure = subparsers.add_parser('configure', help='(re)configure project')
parser_init.add_argument('--generate-config',  help='generate default config.yaml if it does not exist', action='store_true')
parser_configure.set_defaults(configure=True)

argcomplete.autocomplete(parser)
args = vars(parser.parse_args())

def find_root_directory(directory):
    try:
        for root, directories, files in os.walk(directory):
            if '.lpm.yaml' in files:
                return directory
            else:
                return find_root_directory('../' + directory)
    except:
        return False

root_directory = find_root_directory('.')

if 'init' in args:
    if root_directory:
        print ('already initialized in ' + os.path.abspath(root_directory))
    else:
        with open('.lpm.yaml', 'a') as f:
            pass

elif 'configure' in args:
    if root_directory:
        actions.configure()
    else:
        print ('root directory not found')
